package com.enjoy.leetcode;

import java.util.HashSet;
import java.util.Set;

/**
 * 3. 无重复字符的最长子串
 *
 * 滑动窗口
 */
public class ch3 {


    public int lengthOfLongestSubstring(String s) {

        Set<Character> set = new HashSet<>();
        int left = 0;
        int right = 0;
        int max = 0;
        while (right < s.length()) {

            if (!set.contains(s.charAt(right))) {
                set.add(s.charAt(right));
                right++;
            } else {
                // 左边的移掉
                set.remove(s.charAt(left));
                left++;

            }
            max = Math.max(right - left, max);
        }

        return max;

    }


    public static void main(String[] args) {
        ch3 ch3 = new ch3();
        System.out.println("ch3.lengthOfLongestSubstring(\"abcabcbb\") = " + ch3.lengthOfLongestSubstring("abcabcbb"));
    }
}
